//@ts-nocheck
import { defineStore } from "pinia";
import { store } from "@/stores";
import { fetchSsoLogin, ssoLoginParams } from "@/service/user";
import {useAuthInfoHook} from "@/utils/request";
export const useUserInfoStore = defineStore({
    id: "user-info",
    state: () => ({
        tenantId: "",
        userNo: "",
        userName: "",
        accessToken: "",
        refreshToken: "",
        mobile: "",
        openid: "",
        nickName: "微信用户",
        levelName: "普通会员",
        avatarUrl: ""
    }),
    actions: {
        async ssoLogin(data: ssoLoginParams) {
            const res = await fetchSsoLogin(data);
            this.userId = res.data.userId;
            this.accessToken = res.data.accessToken;
            this.refreshToken = res.data.refreshToken;
            this.openid = res.data.openid || "";
            this.expiresTime = res.data.expiresTime;
            const authInfo = useAuthInfoHook();
            authInfo.value.accessToken = res.data.accessToken;
            localStorage.setItem('TOKEN', res.data.accessToken)
        },
        setUserInfo(userInfo: any) {
            this.userNo = userInfo.userNo;
            this.nickName = userInfo.nickName;
            this.userName = userInfo.userName;
            this.levelName = userInfo.levelName;
            this.avatarUrl = userInfo.avatarUrl;
        }
    },
    persist: true,
});

export function useUserInfoStoreHook() {
    return useUserInfoStore(store);
}
